package com.holly.unit.deform;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.holly.unit.db.api.factory.PageFactory;
import com.holly.unit.db.api.factory.PageResultFactory;
import com.holly.unit.db.api.pojo.page.PageResult;
import com.holly.unit.deform.api.DeformUrlAuthApi;
import com.holly.unit.deform.api.exception.DeformException;
import com.holly.unit.deform.api.exception.enums.DeformExceptionEnum;
import com.holly.unit.deform.context.DeformContext;
import com.holly.unit.deform.entity.DesignFormUrlAuth;
import com.holly.unit.deform.service.DeformUrlAuthService;
import com.holly.unit.query.api.QueryApi;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 类描述: 表单设计 URL授权 操作类
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/11 15:15
 */
public class DeformUrlAuthOperator implements DeformUrlAuthApi<DesignFormUrlAuth> {

    private DeformUrlAuthService deformUrlAuthService = DeformContext.deformUrlAuthService();

    private QueryApi queryApi = DeformContext.queryApi();

    @Override
    public boolean initialUrlAuth(String desformCode) {
        return deformUrlAuthService.initialUrlAuth(desformCode);
    }

    @Override
    public boolean urlIsPassed(String desformCode, String urlType) {
        return deformUrlAuthService.urlIsPassed(desformCode,urlType);
    }

    @Override
    public Integer getUrlStatus(String desformCode, String urlType) {
        return deformUrlAuthService.getUrlStatus(desformCode,urlType);
    }

    @Override
    public boolean setUrlStatus(String desformCode, String urlType, Integer status) {
        return deformUrlAuthService.setUrlStatus(desformCode,urlType,status);
    }

    @Override
    public boolean setUrlStatus(LambdaQueryWrapper<DesignFormUrlAuth> wrapper, String urlType, Integer status) {
        return deformUrlAuthService.setUrlStatus(wrapper,urlType,status);
    }

    @Override
    public boolean setAllUrlStatus(String desformCode, Integer... status) {
        return deformUrlAuthService.setAllUrlStatus(desformCode,status);
    }

    @Override
    public List<DesignFormUrlAuth> fetchAllStatusByDesformCode(String desformCode) {
        LambdaQueryWrapper<DesignFormUrlAuth> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DesignFormUrlAuth::getDesformCode, desformCode);
        List<DesignFormUrlAuth> list = this.fetchAllStatusByWrapper(queryWrapper);
        if (list.size() < 3) {
            deformUrlAuthService.initialUrlAuth(desformCode);
            list = deformUrlAuthService.list(queryWrapper);
        }
        return list;
    }

    @Override
    public List<DesignFormUrlAuth> fetchAllStatusByWrapper(LambdaQueryWrapper<DesignFormUrlAuth> queryWrapper) {
        List<DesignFormUrlAuth> list = deformUrlAuthService.list(queryWrapper);
        return list;
    }

    @Override
    public PageResult<DesignFormUrlAuth> page(DesignFormUrlAuth designFormUrlAuth, Map<String, String[]> paramMap) {
        QueryWrapper<DesignFormUrlAuth> queryWrapper = queryApi.initQueryWrapper(designFormUrlAuth, paramMap);
        Page<DesignFormUrlAuth> page = deformUrlAuthService.page(PageFactory.defaultPage(), queryWrapper);
        PageResult<DesignFormUrlAuth> pageResult = PageResultFactory.createPageResult(page);
        return pageResult;
    }

    @Override
    public void delBatch(List<String> ids) {
        if (ObjectUtil.isNull(ids) || ids.size() == 0) {
            throw new DeformException(DeformExceptionEnum.DEFORM_DEL_PARAM_ERROR);
        }
        deformUrlAuthService.removeByIds(ids);
    }

    @Override
    public void delBatch(String ids) {
        if (ids == null || "".equals(ids.trim())) {
            throw new DeformException(DeformExceptionEnum.DEFORM_DEL_PARAM_ERROR);
        } else {
            delBatch(Arrays.asList(ids.split(",")));
        }
    }

    @Override
    public DesignFormUrlAuth fetchEntity(LambdaQueryWrapper<DesignFormUrlAuth> queryWrapper) {
        DesignFormUrlAuth one = deformUrlAuthService.getOne(queryWrapper);
        if (ObjectUtil.isNull(one)) {
            throw new DeformException(DeformExceptionEnum.DEFORM_QUERY_BY_ID_ERROR,one);
        }
        return one;
    }

    @Override
    public DesignFormUrlAuth fetchEntityById(Serializable id) {
        DesignFormUrlAuth one = deformUrlAuthService.getById(id);
        if (ObjectUtil.isNull(one)) {
            throw new DeformException(DeformExceptionEnum.DEFORM_QUERY_BY_ID_ERROR,one);
        }
        return one;
    }

    @Override
    public void saveEntity(DesignFormUrlAuth designFormUrlAuth) {
        deformUrlAuthService.save(designFormUrlAuth);
    }

    @Override
    public boolean editEntity(DesignFormUrlAuth designFormUrlAuth) {
        DesignFormUrlAuth check = deformUrlAuthService.getById(designFormUrlAuth.getDesformId());
        if (ObjectUtil.isNull(check)) throw new DeformException(DeformExceptionEnum.DEFORM_QUERY_BY_ID_ERROR,check);
        return deformUrlAuthService.updateById(designFormUrlAuth);
    }
}
